package com.wzh.glsc.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.to.SkuReductionTo;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;
import com.wzh.glsc.coupon.dao.SkuFullReductionDao;
import com.wzh.glsc.coupon.entity.MemberPriceEntity;
import com.wzh.glsc.coupon.entity.SkuFullReductionEntity;
import com.wzh.glsc.coupon.entity.SkuLadderEntity;
import com.wzh.glsc.coupon.service.MemberPriceService;
import com.wzh.glsc.coupon.service.SkuFullReductionService;
import com.wzh.glsc.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderService skuLadderService;
    @Resource
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        //      折扣、满减、会员价等。glsc-sms库下的
        //                 sms_sku_ladder:折扣、
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderService.saveSkuLadder(skuLadderEntity);

        //                 sms_sku_full_reduction：满减、
        SkuFullReductionEntity skuFullReductionEntity=new SkuFullReductionEntity();
        skuFullReductionEntity.setAddOther(1);
        skuFullReductionEntity.setFullPrice(skuReductionTo.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuReductionTo.getReducePrice());
        skuFullReductionEntity.setSkuId(skuReductionTo.getSkuId());
        baseMapper.insert(skuFullReductionEntity);

        //                 sms_member_price：会员价
        List<MemberPriceEntity> memberPriceEntities=skuReductionTo.getMemberPriceTos().stream().map(item->{
            MemberPriceEntity memberPriceEntity=new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setAddOther(1);
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }
}